package com.example.rightmost.controller.user;

import com.example.rightmost.entity.*;
import com.example.rightmost.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class HomePageController {

    @Autowired
    public HomePageService homePageService;

    //得到粉丝数量
    @RequestMapping("/getFansCount")
    public Integer getFansCount(@RequestParam int id) {
        return homePageService.getFans(id);
    }

    //得到关注数量
    @RequestMapping("/getFollowCount")
    public Integer getFollowCount(@RequestParam int id) {
        return homePageService.getFollow(id);
    }

    //修改信息
    @RequestMapping("/updateInfo")
    public Result updateInfo(@RequestBody User user) {
        return homePageService.updateInfo(user);
    }

    //修改头像
    @RequestMapping("/uploadUserImg")
    public String uploadUserImg(MultipartFile file) {
        if (null == file) {
            return null;
        }
        //获取类加载器资源的路径，并希望在其后拼接"static/upload/"来指定一个文件路径
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "static/upload/";
        System.out.println("---------------------------------------------------------------");
        System.out.println(path);
        path = path.substring(1);//截取字符串下标位1的字符
        System.out.println(path);
        File dir = new File(path);
        //检查dir的目录是否存在，如果不存在，则创建它
        if (!dir.exists()) {
            dir.mkdir();
        }
        //UUID.randomUUID().toString()用于生成一个几乎唯一的字符串
        //file.getOriginalFilename() 是获取上传文件的原始文件名
        String imgName = UUID.randomUUID().toString() + file.getOriginalFilename();
        File imgf = new File(path + imgName);////通过将path和imgName连接起来，得到文件的完整路径
        try {
            file.transferTo(imgf);//上传到服务器所在目录
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgName;
    }

    //我的动态
    @RequestMapping("/getMyPiecesList")
    public List<Piece> getMyPiecesList(@RequestBody Piece piece) {
        List<Piece> pieceList = homePageService.getMyPiecesList(piece);
        return pieceList;
    }

    //判断关注状态
    @RequestMapping("/getFollowInfo")
    public Result getFollowInfo(@RequestBody Fans fans){
        return homePageService.getFollowInfo(fans);
    }

    //关注
    @RequestMapping("/setFollow")
    public Result setFollow(@RequestBody Fans fans) {
        return homePageService.setFollow(fans);
    }

    //取消关注
    @RequestMapping("/delFollow")
    public Result delFollow(@RequestBody Fans fans) {
        return homePageService.delFollow(fans);
    }

}